package cn.iocoder.yudao.module.moa.controller.admin.expensecategory;

import org.springframework.web.bind.annotation.*;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import jakarta.validation.constraints.*;
import jakarta.validation.*;
import jakarta.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.moa.controller.admin.expensecategory.vo.*;
import cn.iocoder.yudao.module.moa.dal.dataobject.expensecategory.ExpenseCategoryDO;
import cn.iocoder.yudao.module.moa.service.expensecategory.ExpenseCategoryService;

@Tag(name = "管理后台 - 费用类型")
@RestController
@RequestMapping("/moa/expense-category")
@Validated
public class ExpenseCategoryController {

    @Resource
    private ExpenseCategoryService expenseCategoryService;

    @PostMapping("/create")
    @Operation(summary = "创建费用类型")
    public CommonResult<String> createExpenseCategory(@Valid @RequestBody ExpenseCategorySaveReqVO createReqVO) {
        return success(expenseCategoryService.createExpenseCategory(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新费用类型")
    public CommonResult<Boolean> updateExpenseCategory(@Valid @RequestBody ExpenseCategorySaveReqVO updateReqVO) {
        expenseCategoryService.updateExpenseCategory(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除费用类型")
    @Parameter(name = "id", description = "编号", required = true)
    public CommonResult<Boolean> deleteExpenseCategory(@RequestParam("id") String id) {
        expenseCategoryService.deleteExpenseCategory(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得费用类型")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    public CommonResult<ExpenseCategoryRespVO> getExpenseCategory(@RequestParam("id") String id) {
        ExpenseCategoryDO expenseCategory = expenseCategoryService.getExpenseCategory(id);
        return success(BeanUtils.toBean(expenseCategory, ExpenseCategoryRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得费用类型分页")
    public CommonResult<PageResult<ExpenseCategoryRespVO>> getExpenseCategoryPage(@Valid ExpenseCategoryPageReqVO pageReqVO) {
        PageResult<ExpenseCategoryDO> pageResult = expenseCategoryService.getExpenseCategoryPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, ExpenseCategoryRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出费用类型 Excel")
    @ApiAccessLog(operateType = EXPORT)
    public void exportExpenseCategoryExcel(@Valid ExpenseCategoryPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<ExpenseCategoryDO> list = expenseCategoryService.getExpenseCategoryPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "费用类型.xls", "数据", ExpenseCategoryRespVO.class,
                        BeanUtils.toBean(list, ExpenseCategoryRespVO.class));
    }

}
